
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>base64 --- Base16, Base32, Base64, Base85 数据编码 &#8212; Python 3.7.3 文档</title>
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <script type="text/javascript" src="../_static/translations.js"></script>
    
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    
    <link rel="search" type="application/opensearchdescription+xml"
          title="在 Python 3.7.3 文档 中搜索"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="关于这些文档" href="../about.html" />
    <link rel="index" title="索引" href="../genindex.html" />
    <link rel="search" title="搜索" href="../search.html" />
    <link rel="copyright" title="版权所有" href="../copyright.html" />
    <link rel="next" title="binhex --- Encode and decode binhex4 files" href="binhex.html" />
    <link rel="prev" title="mimetypes --- Map filenames to MIME types" href="mimetypes.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <link rel="canonical" href="https://docs.python.org/3/library/base64.html" />
    
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    <script type="text/javascript" src="../_static/switchers.js"></script>
    
    
    
    <style>
      @media only screen {
        table.full-width-table {
            width: 100%;
        }
      }
    </style>
 

  </head><body>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             accesskey="I">索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="binhex.html" title="binhex --- Encode and decode binhex4 files"
             accesskey="N">下一页</a> |</li>
        <li class="right" >
          <a href="mimetypes.html" title="mimetypes --- Map filenames to MIME types"
             accesskey="P">上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <span class="language_switcher_placeholder">zh_CN</span>
          <span class="version_switcher_placeholder">3.7.3</span>
          <a href="../index.html">文档</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="netdata.html" accesskey="U">互联网数据处理</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="module-base64">
<span id="base64-base16-base32-base64-base85-data-encodings"></span><h1><a class="reference internal" href="#module-base64" title="base64: RFC 3548: Base16, Base32, Base64 Data Encodings; Base85 and Ascii85"><code class="xref py py-mod docutils literal notranslate"><span class="pre">base64</span></code></a> --- Base16, Base32, Base64, Base85 数据编码<a class="headerlink" href="#module-base64" title="永久链接至标题">¶</a></h1>
<p><strong>源代码：</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.7/Lib/base64.py">Lib/base64.py</a></p>
<hr class="docutils" id="index-0" />
<p>此模块提供了将二进制数据编码为可打印的 ASCII 字符以及将这些编码解码回二进制数据的函数。它为 <span class="target" id="index-16"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc3548.html"><strong>RFC 3548</strong></a> 指定的 Base16, Base32 和 Base64 编码以及已被广泛接受的 Ascii85 和 Base85 编码提供了编码和解码函数。</p>
<p><span class="target" id="index-17"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc3548.html"><strong>RFC 3548</strong></a> 编码的目的是使得二进制数据可以作为电子邮件的内容正确地发送，用作 URL 的一部分，或者作为 HTTP POST 请求的一部分。其中的编码算法和 <strong class="program">uuencode</strong> 程序是不同的。</p>
<p>此模块提供了两个接口。新的接口提供了从 <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">类字节对象</span></a> 到 ASCII 字节 <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> 的编码，以及将 ASCII 的 <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">类字节对象</span></a> 或字符串解码到 <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> 的操作。此模块支持定义在 <span class="target" id="index-18"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc3548.html"><strong>RFC 3548</strong></a> 中的所有 base-64 字母表 （普通的、URL 安全的和文件系统安全的）。</p>
<p>旧的接口不提供从字符串的解码操作，但提供了操作 <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">文件对象</span></a> 的编码和解码函数。旧接口只支持标准的 Base64 字母表，并且按照 <span class="target" id="index-19"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc2045.html"><strong>RFC 2045</strong></a> 的规范每 76 个字符增加一个换行符。注意：如果你需要支持 <span class="target" id="index-20"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc2045.html"><strong>RFC 2045</strong></a>，那么使用 <a class="reference internal" href="email.html#module-email" title="email: Package supporting the parsing, manipulating, and generating email messages."><code class="xref py py-mod docutils literal notranslate"><span class="pre">email</span></code></a> 模块可能更加合适。</p>
<div class="versionchanged">
<p><span class="versionmodified">在 3.3 版更改: </span>新的接口提供的解码函数现在已经支持只包含 ASCII 的 Unicode 字符串。</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">在 3.4 版更改: </span>所有 <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">类字节对象</span></a> 现在已经被所有编码和解码函数接受。添加了对 Ascii85/Base85 的支持。</p>
</div>
<p>新的接口提供：</p>
<dl class="function">
<dt id="base64.b64encode">
<code class="descclassname">base64.</code><code class="descname">b64encode</code><span class="sig-paren">(</span><em>s</em>, <em>altchars=None</em><span class="sig-paren">)</span><a class="headerlink" href="#base64.b64encode" title="永久链接至目标">¶</a></dt>
<dd><p>对 <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> <em>s</em> 进行 Base64 编码，并返回编码后的 <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a>。</p>
<p>可选项 <em>altchars</em> 必须是一个长 2 字节的 <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a>，它指定了用于替换 <code class="docutils literal notranslate"><span class="pre">+</span></code> 和 <code class="docutils literal notranslate"><span class="pre">/</span></code> 的字符。这允许应用程序生成 URL 或文件系统安全的 Base64 字符串。默认值是 <code class="docutils literal notranslate"><span class="pre">None</span></code>，使用标准 Base64 字母表。</p>
</dd></dl>

<dl class="function">
<dt id="base64.b64decode">
<code class="descclassname">base64.</code><code class="descname">b64decode</code><span class="sig-paren">(</span><em>s</em>, <em>altchars=None</em>, <em>validate=False</em><span class="sig-paren">)</span><a class="headerlink" href="#base64.b64decode" title="永久链接至目标">¶</a></dt>
<dd><p>解码 Base64 编码过的 <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> 或 ASCII 字符串 <em>s</em> 并返回解码过的 <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a>。</p>
<p>可选项 <em>altchars</em> 必须是一个长 2 字节的 <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a>，它指定了用于替换 <code class="docutils literal notranslate"><span class="pre">+</span></code> 和 <code class="docutils literal notranslate"><span class="pre">/</span></code> 的字符。</p>
<p>如果 <em>s</em> 被不正确地填写，一个 <a class="reference internal" href="binascii.html#binascii.Error" title="binascii.Error"><code class="xref py py-exc docutils literal notranslate"><span class="pre">binascii.Error</span></code></a> 错误将被抛出。</p>
<p>如果 <em>validate</em> 值为 <code class="docutils literal notranslate"><span class="pre">False</span></code> （默认情况），则在填充检查前，将丢弃既不在标准 base-64 字母表之中也不在备用字母表中的字符。如果 <em>validate</em> 为 <code class="docutils literal notranslate"><span class="pre">True</span></code>，这些非 base64 字符将导致 <a class="reference internal" href="binascii.html#binascii.Error" title="binascii.Error"><code class="xref py py-exc docutils literal notranslate"><span class="pre">binascii.Error</span></code></a>。</p>
</dd></dl>

<dl class="function">
<dt id="base64.standard_b64encode">
<code class="descclassname">base64.</code><code class="descname">standard_b64encode</code><span class="sig-paren">(</span><em>s</em><span class="sig-paren">)</span><a class="headerlink" href="#base64.standard_b64encode" title="永久链接至目标">¶</a></dt>
<dd><p>编码 <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> <em>s</em>，使用标准 Base64 字母表并返回编码过的 <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a>。</p>
</dd></dl>

<dl class="function">
<dt id="base64.standard_b64decode">
<code class="descclassname">base64.</code><code class="descname">standard_b64decode</code><span class="sig-paren">(</span><em>s</em><span class="sig-paren">)</span><a class="headerlink" href="#base64.standard_b64decode" title="永久链接至目标">¶</a></dt>
<dd><p>解码 <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> 或 ASCII 字符串 <em>s</em>，使用标准 Base64 字母表并返回编码过的 <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a>。</p>
</dd></dl>

<dl class="function">
<dt id="base64.urlsafe_b64encode">
<code class="descclassname">base64.</code><code class="descname">urlsafe_b64encode</code><span class="sig-paren">(</span><em>s</em><span class="sig-paren">)</span><a class="headerlink" href="#base64.urlsafe_b64encode" title="永久链接至目标">¶</a></dt>
<dd><p>编码 <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> <em>s</em>，使用 URL 与文件系统安全的字母表，使用 <code class="docutils literal notranslate"><span class="pre">-</span></code> 以及 <code class="docutils literal notranslate"><span class="pre">_</span></code> 代替标准 Base64 字母表中的 <code class="docutils literal notranslate"><span class="pre">+</span></code> 和 <code class="docutils literal notranslate"><span class="pre">/</span></code>。返回编码过的 <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a>。结果中可能包含 <code class="docutils literal notranslate"><span class="pre">=</span></code>。</p>
</dd></dl>

<dl class="function">
<dt id="base64.urlsafe_b64decode">
<code class="descclassname">base64.</code><code class="descname">urlsafe_b64decode</code><span class="sig-paren">(</span><em>s</em><span class="sig-paren">)</span><a class="headerlink" href="#base64.urlsafe_b64decode" title="永久链接至目标">¶</a></dt>
<dd><p>解码 <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> 或 ASCII 字符串 <em>s</em>，使用 URL 与文件系统安全的字母表，使用 <code class="docutils literal notranslate"><span class="pre">-</span></code> 以及 <code class="docutils literal notranslate"><span class="pre">_</span></code> 代替标准 Base64 字母表中的 <code class="docutils literal notranslate"><span class="pre">+</span></code> 和 <code class="docutils literal notranslate"><span class="pre">/</span></code>。返回解码过的 <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a></p>
</dd></dl>

<dl class="function">
<dt id="base64.b32encode">
<code class="descclassname">base64.</code><code class="descname">b32encode</code><span class="sig-paren">(</span><em>s</em><span class="sig-paren">)</span><a class="headerlink" href="#base64.b32encode" title="永久链接至目标">¶</a></dt>
<dd><p>用 Base32 编码 <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> <em>s</em> 并返回编码过的 <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a></p>
</dd></dl>

<dl class="function">
<dt id="base64.b32decode">
<code class="descclassname">base64.</code><code class="descname">b32decode</code><span class="sig-paren">(</span><em>s</em>, <em>casefold=False</em>, <em>map01=None</em><span class="sig-paren">)</span><a class="headerlink" href="#base64.b32decode" title="永久链接至目标">¶</a></dt>
<dd><p>解码 Base32 编码过的 <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> 或 ASCII 字符串 <em>s</em> 并返回解码过的 <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a>。</p>
<p>可选的 <em>casefold</em> 是一个指定小写字幕是否可接受为输入的标志。为了安全考虑，默认值为 <code class="docutils literal notranslate"><span class="pre">False</span></code>。</p>
<p><span class="target" id="index-21"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc3548.html"><strong>RFC 3548</strong></a> 允许将字母 0(zero) 映射为字母 O(oh)，并可以选择是否将字母 1(one) 映射为 I(eye) 或 L(el)。可选参数 <em>map01</em> 不是 <code class="docutils literal notranslate"><span class="pre">None</span></code> 时，它的值指定 1 的映射目标 (I 或 l)，当 <em>map01</em> 非 <code class="docutils literal notranslate"><span class="pre">None</span></code> 时， 0 总是被映射为 O。为了安全考虑，默认值被设为 <code class="docutils literal notranslate"><span class="pre">None</span></code>，如果是这样， 0 和 1 不允许被作为输入。</p>
<p>如果 <em>s</em> 被错误地填写或输入中存在字母表之外的字符，将抛出 <a class="reference internal" href="binascii.html#binascii.Error" title="binascii.Error"><code class="xref py py-exc docutils literal notranslate"><span class="pre">binascii.Error</span></code></a>。</p>
</dd></dl>

<dl class="function">
<dt id="base64.b16encode">
<code class="descclassname">base64.</code><code class="descname">b16encode</code><span class="sig-paren">(</span><em>s</em><span class="sig-paren">)</span><a class="headerlink" href="#base64.b16encode" title="永久链接至目标">¶</a></dt>
<dd><p>用 Base16 编码 <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> <em>s</em> 并返回编码过的 <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a></p>
</dd></dl>

<dl class="function">
<dt id="base64.b16decode">
<code class="descclassname">base64.</code><code class="descname">b16decode</code><span class="sig-paren">(</span><em>s</em>, <em>casefold=False</em><span class="sig-paren">)</span><a class="headerlink" href="#base64.b16decode" title="永久链接至目标">¶</a></dt>
<dd><p>解码 Base16 编码过的 <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> 或 ASCII 字符串 <em>s</em> 并返回解码过的 <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a>。</p>
<p>可选的 <em>casefold</em> 是一个指定小写字幕是否可接受为输入的标志。为了安全考虑，默认值为 <code class="docutils literal notranslate"><span class="pre">False</span></code>。</p>
<p>如果 <em>s</em> 被错误地填写或输入中存在字母表之外的字符，将抛出 <a class="reference internal" href="binascii.html#binascii.Error" title="binascii.Error"><code class="xref py py-exc docutils literal notranslate"><span class="pre">binascii.Error</span></code></a>。</p>
</dd></dl>

<dl class="function">
<dt id="base64.a85encode">
<code class="descclassname">base64.</code><code class="descname">a85encode</code><span class="sig-paren">(</span><em>b</em>, <em>*</em>, <em>foldspaces=False</em>, <em>wrapcol=0</em>, <em>pad=False</em>, <em>adobe=False</em><span class="sig-paren">)</span><a class="headerlink" href="#base64.a85encode" title="永久链接至目标">¶</a></dt>
<dd><p>用 Ascii85 编码 <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> <em>s</em> 并返回编码过的 <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a></p>
<p><em>foldspaces</em> 是一个可选的标志，使用特殊的短序列 'y' 代替 'btoa' 提供的 4 个连续空格 (ASCII 0x20)。这个特性不被 &quot;标准&quot; Ascii85 编码支持。</p>
<p><em>wrapcol</em> 控制了输出是否包含换行符 (<code class="docutils literal notranslate"><span class="pre">b'\n'</span></code>). 如果该值非零, 则每一行只有该值所限制的字符长度.</p>
<p><em>pad</em> 控制在编码之前输入是否填充为4的倍数。请注意，<code class="docutils literal notranslate"><span class="pre">btoa</span></code> 实现总是填充。</p>
<p><em>adobe</em> controls whether the encoded byte sequence is framed with <code class="docutils literal notranslate"><span class="pre">&lt;~</span></code>
and <code class="docutils literal notranslate"><span class="pre">~&gt;</span></code>, which is used by the Adobe implementation.</p>
<div class="versionadded">
<p><span class="versionmodified">3.4 新版功能.</span></p>
</div>
</dd></dl>

<dl class="function">
<dt id="base64.a85decode">
<code class="descclassname">base64.</code><code class="descname">a85decode</code><span class="sig-paren">(</span><em>b</em>, <em>*</em>, <em>foldspaces=False</em>, <em>adobe=False</em>, <em>ignorechars=b' \t\n\r\v'</em><span class="sig-paren">)</span><a class="headerlink" href="#base64.a85decode" title="永久链接至目标">¶</a></dt>
<dd><p>解码 Ascii85 编码过的 <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> 或 ASCII 字符串 <em>s</em> 并返回解码过的 <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a>。</p>
<p><em>foldspaces</em> is a flag that specifies whether the 'y' short sequence
should be accepted as shorthand for 4 consecutive spaces (ASCII 0x20).
This feature is not supported by the &quot;standard&quot; Ascii85 encoding.</p>
<p><em>adobe</em> controls whether the input sequence is in Adobe Ascii85 format
(i.e. is framed with &lt;~ and ~&gt;).</p>
<p><em>ignorechars</em> should be a <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> or ASCII string
containing characters to ignore
from the input. This should only contain whitespace characters, and by
default contains all whitespace characters in ASCII.</p>
<div class="versionadded">
<p><span class="versionmodified">3.4 新版功能.</span></p>
</div>
</dd></dl>

<dl class="function">
<dt id="base64.b85encode">
<code class="descclassname">base64.</code><code class="descname">b85encode</code><span class="sig-paren">(</span><em>b</em>, <em>pad=False</em><span class="sig-paren">)</span><a class="headerlink" href="#base64.b85encode" title="永久链接至目标">¶</a></dt>
<dd><p>Encode the <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> <em>b</em> using base85 (as used in e.g.
git-style binary diffs) and return the encoded <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a>.</p>
<p>If <em>pad</em> is true, the input is padded with <code class="docutils literal notranslate"><span class="pre">b'\0'</span></code> so its length is a
multiple of 4 bytes before encoding.</p>
<div class="versionadded">
<p><span class="versionmodified">3.4 新版功能.</span></p>
</div>
</dd></dl>

<dl class="function">
<dt id="base64.b85decode">
<code class="descclassname">base64.</code><code class="descname">b85decode</code><span class="sig-paren">(</span><em>b</em><span class="sig-paren">)</span><a class="headerlink" href="#base64.b85decode" title="永久链接至目标">¶</a></dt>
<dd><p>Decode the base85-encoded <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> or ASCII string <em>b</em> and
return the decoded <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a>.  Padding is implicitly removed, if
necessary.</p>
<div class="versionadded">
<p><span class="versionmodified">3.4 新版功能.</span></p>
</div>
</dd></dl>

<p>The legacy interface:</p>
<dl class="function">
<dt id="base64.decode">
<code class="descclassname">base64.</code><code class="descname">decode</code><span class="sig-paren">(</span><em>input</em>, <em>output</em><span class="sig-paren">)</span><a class="headerlink" href="#base64.decode" title="永久链接至目标">¶</a></dt>
<dd><p>Decode the contents of the binary <em>input</em> file and write the resulting binary
data to the <em>output</em> file. <em>input</em> and <em>output</em> must be <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file objects</span></a>. <em>input</em> will be read until <code class="docutils literal notranslate"><span class="pre">input.readline()</span></code> returns an
empty bytes object.</p>
</dd></dl>

<dl class="function">
<dt id="base64.decodebytes">
<code class="descclassname">base64.</code><code class="descname">decodebytes</code><span class="sig-paren">(</span><em>s</em><span class="sig-paren">)</span><a class="headerlink" href="#base64.decodebytes" title="永久链接至目标">¶</a></dt>
<dd><p>Decode the <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> <em>s</em>, which must contain one or more
lines of base64 encoded data, and return the decoded <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a>.</p>
<div class="versionadded">
<p><span class="versionmodified">3.1 新版功能.</span></p>
</div>
</dd></dl>

<dl class="function">
<dt id="base64.decodestring">
<code class="descclassname">base64.</code><code class="descname">decodestring</code><span class="sig-paren">(</span><em>s</em><span class="sig-paren">)</span><a class="headerlink" href="#base64.decodestring" title="永久链接至目标">¶</a></dt>
<dd><p>Deprecated alias of <a class="reference internal" href="#base64.decodebytes" title="base64.decodebytes"><code class="xref py py-func docutils literal notranslate"><span class="pre">decodebytes()</span></code></a>.</p>
<div class="deprecated">
<p><span class="versionmodified">3.1 版后已移除.</span></p>
</div>
</dd></dl>

<dl class="function">
<dt id="base64.encode">
<code class="descclassname">base64.</code><code class="descname">encode</code><span class="sig-paren">(</span><em>input</em>, <em>output</em><span class="sig-paren">)</span><a class="headerlink" href="#base64.encode" title="永久链接至目标">¶</a></dt>
<dd><p>Encode the contents of the binary <em>input</em> file and write the resulting base64
encoded data to the <em>output</em> file. <em>input</em> and <em>output</em> must be <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file
objects</span></a>. <em>input</em> will be read until <code class="docutils literal notranslate"><span class="pre">input.read()</span></code> returns
an empty bytes object. <a class="reference internal" href="#base64.encode" title="base64.encode"><code class="xref py py-func docutils literal notranslate"><span class="pre">encode()</span></code></a> inserts a newline character (<code class="docutils literal notranslate"><span class="pre">b'\n'</span></code>)
after every 76 bytes of the output, as well as ensuring that the output
always ends with a newline, as per <span class="target" id="index-7"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc2045.html"><strong>RFC 2045</strong></a> (MIME).</p>
</dd></dl>

<dl class="function">
<dt id="base64.encodebytes">
<code class="descclassname">base64.</code><code class="descname">encodebytes</code><span class="sig-paren">(</span><em>s</em><span class="sig-paren">)</span><a class="headerlink" href="#base64.encodebytes" title="永久链接至目标">¶</a></dt>
<dd><p>Encode the <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> <em>s</em>, which can contain arbitrary binary
data, and return <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> containing the base64-encoded data, with newlines
(<code class="docutils literal notranslate"><span class="pre">b'\n'</span></code>) inserted after every 76 bytes of output, and ensuring that
there is a trailing newline, as per <span class="target" id="index-8"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc2045.html"><strong>RFC 2045</strong></a> (MIME).</p>
<div class="versionadded">
<p><span class="versionmodified">3.1 新版功能.</span></p>
</div>
</dd></dl>

<dl class="function">
<dt id="base64.encodestring">
<code class="descclassname">base64.</code><code class="descname">encodestring</code><span class="sig-paren">(</span><em>s</em><span class="sig-paren">)</span><a class="headerlink" href="#base64.encodestring" title="永久链接至目标">¶</a></dt>
<dd><p>Deprecated alias of <a class="reference internal" href="#base64.encodebytes" title="base64.encodebytes"><code class="xref py py-func docutils literal notranslate"><span class="pre">encodebytes()</span></code></a>.</p>
<div class="deprecated">
<p><span class="versionmodified">3.1 版后已移除.</span></p>
</div>
</dd></dl>

<p>An example usage of the module:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">base64</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">encoded</span> <span class="o">=</span> <span class="n">base64</span><span class="o">.</span><span class="n">b64encode</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39;data to be encoded&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">encoded</span>
<span class="go">b&#39;ZGF0YSB0byBiZSBlbmNvZGVk&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">data</span> <span class="o">=</span> <span class="n">base64</span><span class="o">.</span><span class="n">b64decode</span><span class="p">(</span><span class="n">encoded</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">data</span>
<span class="go">b&#39;data to be encoded&#39;</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="first admonition-title">参见</p>
<dl class="last docutils">
<dt>模块 <a class="reference internal" href="binascii.html#module-binascii" title="binascii: Tools for converting between binary and various ASCII-encoded binary representations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">binascii</span></code></a></dt>
<dd>Support module containing ASCII-to-binary and binary-to-ASCII conversions.</dd>
<dt><span class="target" id="index-9"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc1521.html"><strong>RFC 1521</strong></a> - MIME (Multipurpose Internet Mail Extensions) Part One: Mechanisms for Specifying and Describing the Format of Internet Message Bodies</dt>
<dd>Section 5.2, &quot;Base64 Content-Transfer-Encoding,&quot; provides the definition of the
base64 encoding.</dd>
</dl>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h4>上一个主题</h4>
  <p class="topless"><a href="mimetypes.html"
                        title="上一章"><code class="docutils literal notranslate"><span class="pre">mimetypes</span></code> --- Map filenames to MIME types</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="binhex.html"
                        title="下一章"><code class="docutils literal notranslate"><span class="pre">binhex</span></code> --- Encode and decode binhex4 files</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../bugs.html">提交 Bug</a></li>
      <li>
        <a href="https://github.com/python/cpython/blob/3.7/Doc/library/base64.rst"
            rel="nofollow">显示源代码
        </a>
      </li>
    </ul>
  </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             >索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="binhex.html" title="binhex --- Encode and decode binhex4 files"
             >下一页</a> |</li>
        <li class="right" >
          <a href="mimetypes.html" title="mimetypes --- Map filenames to MIME types"
             >上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <span class="language_switcher_placeholder">zh_CN</span>
          <span class="version_switcher_placeholder">3.7.3</span>
          <a href="../index.html">文档</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="netdata.html" >互联网数据处理</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">版权所有</a> 2001-2019, Python Software Foundation.
    <br />
    Python 软件基金会是一个非盈利组织。
    <a href="https://www.python.org/psf/donations/">请捐助。</a>
    <br />
    最后更新于 4月 09, 2019.
    <a href="../bugs.html">发现了问题</a>？
    <br />
    使用<a href="http://sphinx.pocoo.org/">Sphinx</a>1.8.4 创建。
    </div>

  </body>
</html>